package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.dto.UserGrantDTO;
import com.neuedu.yyzx.po.Role;
import com.neuedu.yyzx.po.User;
import com.neuedu.yyzx.service.RoleService;
import com.neuedu.yyzx.service.UserService;
import com.neuedu.yyzx.vo.UserInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 金山
 * 项目：yyzx
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/2514:23
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;


    @Autowired
    private PasswordEncoder passwordEncoder;


    /**
     * http://localhost:8080/user/list
     *
     * @return
     */
    @GetMapping("/list")
    public Ret list() {


        return Ret.ok(userService.list());
    }

    @GetMapping("/div/{numa}/{numb}")
    public Ret div(@PathVariable int numa, @PathVariable int numb) {


        // Ret ret =  new Ret();
        // ret.setCode(200);
        // ret.setData(numa / numb);
        // return ret;

        return Ret.ok(numa / numb);
    }


    @GetMapping("/page")
    public Ret<List<User>> page(Page<User> page, User user) {


        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (user.getNickname() != null && !"".equals(user.getNickname().trim())) {
            queryWrapper.like(User::getNickname, user.getNickname());   //  where  nickname like  ?
        }

        // ORDER BY createtime DESC
        queryWrapper.orderByDesc(User::getCreateTime);


        page = userService.page(page, queryWrapper);
        return Ret.ok(page);
    }


    /**
     * 用户的添加或者更细
     *
     * @param user
     * @return
     */
    @Operation(summary = "用户保存或者更新")
    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody User user) {
        // 将浏览器的明文 转换成加密 的 秘闻
        String rawPassword = user.getPassword();

        // 设置成秘闻
        user.setPassword(passwordEncoder.encode(rawPassword));
        System.out.println("user.getPassword() = " + user.getPassword());


        boolean success = userService.saveOrUpdate(user);
        return Ret.ok(success);
    }


    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Ret<User> get(@PathVariable int id) {
        User user = userService.getById(id);
        return Ret.ok(user);
    }


    /**
     * 根据主键删除
     * <p>
     * /user/id
     * method delete
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        return Ret.ok(userService.removeById(id));
    }


    @GetMapping("/getRoleList/{userId}")
    public Ret<List<Role>> getRoleList(@PathVariable long userId) {
        Map map = new HashMap();
        // 当前用户信息
        map.put("user", userService.getById(userId));

        // 用户拥有的橘色
        map.put("userRoleList", roleService.queryUserRoleList(userId));

        // 所有角色
        map.put("roleList", roleService.list());

        return Ret.ok(map);
    }


    /**
     * 授权
     *
     * @param dto
     * @return
     */
    @PostMapping("/grant")
    public Ret<Boolean> getRolgranteList(@RequestBody UserGrantDTO dto) throws Exception {

        boolean success = userService.grant(dto);

        return Ret.ok(success);
    }


    /**
     * 获取用户身份、昵称、权限菜单
     *
     * @param token
     * @return
     */
    @GetMapping("/info")
    public Ret<UserInfoVO> info(@RequestHeader("Authorization") String token) {
        return Ret.ok(userService.info(token));
    }


}
